package com.ovhoo.android.fiveexplore;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ovhoo.android.fiveexplore.R;

import com.ovhoo.android.file.CurentDirManager;
import com.ovhoo.android.file.FileInterface;

public class ImageViewerDialog extends AbstractOpenFileDialog implements OnClickListener{
	
	private static final int HANDLER_MSG_CONFIRM_RENAME = 0;
	private static final int HANDLER_MSG_CONFIRM_DELETE = 1;
	
	private ImageView imageView;
	
	private TextView fileBaseName;
	private Button openFile, suppressFile, renameFile, cancelButton;
	
	public ImageViewerDialog(Context context, CurentDirManager fileManager,
			FileInterface file) {
		super(context, fileManager, file);
		this.buildUI(); //Creation of the UI
	}

	@Override
	protected void buildUI() {
		LayoutInflater _inflater=this.getLayoutInflater();
		View _elementView = _inflater.inflate(R.layout.imageview, null);
		
		this.imageView = (ImageView) _elementView.findViewById(R.id.IMAGE_VIEW_IMAGE);
		
		this.fileBaseName = (TextView) _elementView.findViewById(R.id.IMAGE_VIEW_IMAGE_BASENAME);
		
		
		//Buttons
		this.openFile = (Button) _elementView.findViewById(R.id.OPEN_FILE_OPEN);
		this.renameFile = (Button) _elementView.findViewById(R.id.OPEN_FILE_RENAME);
		this.suppressFile = (Button) _elementView.findViewById(R.id.OPEN_FILE_DELETE);
		this.cancelButton = (Button) _elementView.findViewById(R.id.OPEN_FILE_CANCEL);
		
		//Listener
		this.openFile.setOnClickListener(this);
		this.renameFile.setOnClickListener(this);
		this.suppressFile.setOnClickListener(this);
		this.cancelButton.setOnClickListener(this);
		
		this.setContentView(_elementView);

	}
	
	public void show(){
		Bitmap _image = BitmapFactory.decodeFile(this.file.getAbsolutePath());
		
		this.imageView.setImageBitmap(_image);
		
		this.fileBaseName.setText(file.getName());
		
		
		//Hide Open button for unknown files
		if (mimeTypes.getMimeType(file) == null){
			this.openFile.setEnabled(false);
		}else if(mimeTypes.getMimeType(file).length()==0){
			this.openFile.setEnabled(false);
		}
		else{
			this.openFile.setEnabled(true);
		}
		super.show();
	}
	
	@Override
	public void onClick(View v) {
		if (v == this.openFile){
			this.onOpenFile();
		}
		else if (v == this.renameFile){
			this.onRenameFile();
		}
		else if (v == this.suppressFile){
			this.onSuppressFile();
		}
		else if(v == this.cancelButton){
			this.dismiss();
		}
	}
	
	protected boolean onOpenFile(){
		try {
			Intent myIntent =new Intent(android.content.Intent.ACTION_VIEW);
			myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			myIntent.setDataAndType(file.getUri(), this.mimeTypes.getMimeType(file) );
			this.getContext().startActivity(myIntent);
			
			return true;
		} catch( ActivityNotFoundException e){
			this.showAlertMessage(this.getContext().getResources().getString(R.string.OPEN_FILE_CANNOT_OPEN));
			System.err.println("OpenFileDialog::onClick ActivityNotFoundException ");
			e.printStackTrace();
			return false;
		}
	}
	
	
	protected void onRenameFile(){
		RenameFileDialog _dialog = new RenameFileDialog(this.getContext(), this.handler.obtainMessage(HANDLER_MSG_CONFIRM_RENAME, null), this.file);
		_dialog.show();
	}
	
	protected void onSuppressFile(){
		DeleteFileDialog _dialog = new DeleteFileDialog(this.getContext(), this.handler.obtainMessage(HANDLER_MSG_CONFIRM_DELETE, null), this.file);
		_dialog.show();
	}
	
	/**
	 * Display an alert message
	 * @param msg the alert message to be displayed
	 */
	protected void showAlertMessage(String msg){
		
		this.alertDialog.setMessage(msg);
		this.alertDialog.show();
	}
	
	Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			
			if (msg != null){
				switch(msg.what){
					case HANDLER_MSG_CONFIRM_RENAME : this.rename((String) msg.obj); break;
					case HANDLER_MSG_CONFIRM_DELETE : this.delete(); break;
				}
				
			}
			
		}
		
		public void rename( String baseName){
			if (fileManager.rename(file, baseName)){
				dismiss();
			}
			else{
				showAlertMessage(getContext().getResources().getString(R.string.OPEN_FILE_CANNOT_RENAME));
			}
			
		}
		
		public void delete(){
			if (fileManager.rm(file)){
				dismiss();
			}
			else{
				showAlertMessage(getContext().getResources().getString(R.string.OPEN_FILE_CANNOT_DELTE));
			}
		}
	};

}
